package com.yitiao.designmodel.create.prototype;

import java.io.*;

/**
 * author:一条
 */
public class MainTest {
    public static void main(String[] args) throws CloneNotSupportedException, IOException, ClassNotFoundException {
//        YitiaoBatis yitiaoBatis = new YitiaoBatis();
//        Yitiao yitiao1 = yitiaoBatis.getYitiao("yitiao");
//        System.out.println("第一次查询："+yitiao1);
//        Yitiao yitiao2 = yitiaoBatis.getYitiao("yitiao");
//        System.out.println("第二次查询："+yitiao2);
//        //执行后续业务，修改id
//        yitiao2.setId(100);
//        yitiao2.setAge(0);
//        Yitiao yitiao3 = yitiaoBatis.getYitiao("yitiao");
//        System.out.println("第三次查询："+yitiao3);

          //clone方式的浅拷贝
//        Yitiao yitiao1 = new Yitiao();
//        Age age = new Age(1);
//        yitiao1.setAge(age);
//        yitiao1.setId(1);
//        Yitiao clone = yitiao1.clone();
//        yitiao1.setId(2);
//        age.setAge(2);
//        System.out.println("yitiao1:\n"+yitiao1+"\nclone:\n"+clone);

            //构造方法
//        Yitiao yitiao1 = new Yitiao();
//        Age age = new Age(1);
//        yitiao1.setAge(age);
//        yitiao1.setId(1);
//        Yitiao clone = new Yitiao(yitiao1);
//        yitiao1.setId(2);
//        age.setAge(2);
//        System.out.println("yitiao1:\n"+yitiao1+"\nclone:\n"+clone);

        //通过对象序列化实现深拷贝
        Yitiao yitiao = new Yitiao();
        Age age = new Age(1);
        yitiao.setAge(age);
        yitiao.setId(1);
        ByteArrayOutputStream bos=new ByteArrayOutputStream();
        ObjectOutputStream oos=new ObjectOutputStream(bos);
        oos.writeObject(yitiao);
        oos.flush();
        ObjectInputStream ois=new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
        Yitiao clone = (Yitiao) ois.readObject();
        yitiao.setId(2);
        age.setAge(2);
        System.out.println("yitiao:\n"+yitiao+"\nclone:\n"+clone);
    }
}
